#include <vector>
#include <iostream>
#include <sstream>

int main()
{
    int N;
    int K;
    std::cin >> N;
    std::cin >> K;
    std::cin.ignore();

    std::vector<std::vector<int>> in_table;
    std::string s;
    while (std::getline(std::cin, s))
    {
        std::istringstream iss(s);
        int n;
        std::vector<int> nv;
        while (iss >> n)
        {
            nv.push_back(n);
        }
        in_table.push_back(nv);
    }

    std::vector<std::vector<int>> win_table(K, std::vector<int>(N));
    std::vector<int> idx(in_table.size(), 0);
    int now_list = 0;
    for (int i = 0; i < K; i++)
    {
        int st = now_list;
        for (int j = 0; j < N; j++)
        {
            while (idx[now_list] >= in_table[now_list].size())
                now_list = (now_list + 1) % in_table.size();
            win_table[i][j] = (in_table[now_list][idx[now_list]]);
            idx[now_list]++;
        }

        if (i == K - 1)
            break;
        if (st == now_list)
        {
            int nex = (now_list + 1) % in_table.size();
            while (idx[nex] >= in_table[nex].size())
                nex = (nex + 1) % in_table.size();
            now_list = nex;
        }
    }

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < K; j++)
        {
            std::cout << win_table[j][i] << " ";
        }
    }
    std::cout << std::endl;

    return 0;
}